﻿

CREATE PROCEDURE [Admin].[sp_z4s_duplicate_testSession]
	@testSession_id uniqueidentifier, 
	@scheduledStartDateTime datetime,
	@newTestSession_id uniqueidentifier = NULL OUTPUT,
	@newTestSession_name nvarchar(255) = NULL,
	@newTestSession_createdOn datetime
AS

	IF @newTestSession_id IS NULL SET @newTestSession_id = NEWID()

	INSERT INTO [Admin].[testSession] 
		([testSession_id]
		,[testSession_name]
		,[testSession_createdOn]
		,[testSession_testSessionCategoryId]
		,[testSession_testSessionStateId])
		SELECT
			 @newTestSession_id AS testSession_id
			,CASE WHEN @newTestSession_name IS NULL OR LTRIM(RTRIM(@newTestSession_name)) = '' THEN testSession_name ELSE @newTestSession_name END AS testSession_name
			,@newTestSession_createdOn AS testSession_createdOn
			,testSession_testSessionCategoryId AS testSession_testSessionCategoryId
			,'9F5E44E3-96EC-467D-A0C7-A7E94D3C8B88'
		FROM [Admin].[testSession] 
		WHERE testSession_id = @testSession_id

	DECLARE @minScheduledStartDateTime datetime
	SELECT @minScheduledStartDateTime = MIN([clientSession_scheduledStartDateTime])
	FROM [Admin].[clientSession]
	WHERE [clientSession_testSessionId] = @testSession_id

	DECLARE @clientSessionMapping TABLE(oldClientSession_id uniqueidentifier, newClientSession_id uniqueidentifier, delta int)
	INSERT INTO @clientSessionMapping
		SELECT clientSession_id, NEWID(), DATEDIFF(second, @minScheduledStartDateTime, clientSession_scheduledStartDateTime)
		FROM [Admin].[clientSession]
		WHERE [clientSession_testSessionId] = @testSession_id  

	INSERT INTO [Admin].[clientSession]
		([clientSession_id]
		,[clientSession_name]
		,[clientSession_testServerId]
		,[clientSession_testSessionId]
		,[clientSession_enableLog]
		,[clientSession_scheduledStartDateTime]
		,[clientSession_testScenarioId]
		,[clientSession_readyToStart]
		,[clientSession_readyToStartDateTime]
		,[clientSession_readyToStartDateTimeOnServer]
		,[clientSession_processId]
		,[clientSession_startDateTime]
		,[clientSession_startDateTimeOnServer]
		,[clientSession_endDateTime]
		,[clientSession_endDateTimeOnServer]
		,[clientSession_error])
		SELECT
			 (SELECT newClientSession_id FROM @clientSessionMapping WHERE oldClientSession_id = [clientSession_id]) AS [clientSession_id]
			,[clientSession_name]
			,[clientSession_testServerId]
			,@newTestSession_id
			,[clientSession_enableLog]
			,(SELECT DATEADD(second, delta, @scheduledStartDateTime) FROM @clientSessionMapping WHERE oldClientSession_id = [clientSession_id])AS [clientSession_scheduledStartDateTime]
			,[clientSession_testScenarioId]
			,'1'
			,NULL
			,NULL
			,NULL
			,NULL
			,NULL
			,NULL
			,NULL
			,NULL
		FROM [Admin].[clientSession]
		WHERE [clientSession_testSessionId] = @testSession_id

	INSERT INTO [Scenario].[activityContext]
		([activityContext_id]
		,[activityContext_activityId]
		,[activityContext_clientSessionId]
		,[activityContext_contextItemId]
		,[activityContext_overridingData])
		SELECT
			 NEWID() AS [activityContext_id]
			,[activityContext_activityId]
			,(SELECT newClientSession_id FROM @clientSessionMapping WHERE oldClientSession_id = [activityContext_clientSessionId]) AS [activityContext_clientSessionId]
			,[activityContext_contextItemId]
			,[activityContext_overridingData]
		FROM [Scenario].[activityContext]
		WHERE [activityContext_clientSessionId] IN 
			(SELECT [clientSession_id] FROM [Admin].[clientSession]	WHERE [clientSession_testSessionId] = @testSession_id)
